package com.suxin.shiro.main;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.*;
import org.apache.shiro.realm.Realm;

/**
 * @author Tang
 * @classname RealmOne
 * @description [  ]
 * @date 2021/3/18 9:36
 */
@Slf4j
public class RealmOne implements Realm {

    /**
     * 唯一的名字
     * @return
     */
    @Override
    public String getName() {
        return "RealmOne";
    }

    /**
     * realm 是否支持此token
     * @param token
     * @return
     */
    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof UsernamePasswordToken;
    }

    @Override
    public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        log.info("验证身份开始...");
        String username = token.getPrincipal().toString();
        String password = new String((char[]) token.getCredentials());
        if (!"zhang".equals(username)) {
            throw new UnknownAccountException();
        }
        if (!"123".equals(password)) {
            throw new IncorrectCredentialsException();
        }
        // 身份认证成功，返回一个 AuthenticationInfo 实现
        return new SimpleAuthenticationInfo(username,password,getName());
    }
}